package cn.javadog.algorithm.e601_1000;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 数组形式的整数加法
 * https://leetcode-cn.com/problems/add-to-array-form-of-integer/
 * @author Sprite
 */
public class Algorithm989 {

	public static void main(String[] args) {
		List<Integer> list = addToArrayForm(new int[]{9, 9, 9, 9, 9, 9, 9, 9, 9, 9}, 1);
		System.out.println(list);
	}

	public static List<Integer> addToArrayForm(int[] A, int K) {
		String ks = String.valueOf(K);
		int kLen = ks.length();
		int aLen = A.length;
		int maxLen = Math.max(aLen, kLen);
		List<Integer> list = new ArrayList<>(maxLen);
		int ak10 = 0;
		for (int i = 0; i < maxLen; i++) {
			int a = aLen >i ? A[aLen-1-i] : 0;
			int k = kLen>i ? Integer.parseInt(ks.substring(kLen-1-i,kLen-i)) : 0;
			int ak = a+k+ak10;
			list.add(ak%10);
			ak10 = ak/10;
		}
		if (ak10 != 0){
			list.add(ak10);
		}
		Collections.reverse(list);
		return list;
	}

}
